# -*-coding:utf-8-*-
# ProjectName: smalleel-api-tools
# FileName: db_retrieve.py
# @Time :2023/7/21 11:07
# @Author :
# @Description:电表余额退补

from common.util.jsonUtil.jsonUtil import read_sql_json, read_extract_json
from common.util.mySqlUtil.mySqlUtil import select_sql
from common.util.requestUtil.requestUtil import RequestsUtil
from common.util.timeUntil.timeUtil import get_last_date
from tool.process.config.smalleelApi import *
from tool.process.smalleelLogin import Login
from tool.process.config.retrieveConfig import RetrieveConfig


class DbRetrieve:
    def __init__(self):
        self.SmalleelApi = SmalleelApi()
        self.PlatformApi = PlatformApi()
        self.req = RequestsUtil()
        self.config = RetrieveConfig()
        self.smalleel_token = read_extract_json('tokenPro')
        self.smalleel_optPwd = read_extract_json('optPwdPro')

    def __cost_detail(self, startTime=get_last_date(1), endTime=get_last_date(1), plusPrice=0.5,
                      priceContent=1.1):
        url = self.SmalleelApi.costdetail
        headers = {"Authorization": self.smalleel_token}
        if self.config.priceType == 2:
            priceContent = "{\"1\":\"1.1\",\"2\":\"2.2\",\"3\":\"3.3\",\"4\":\"4.4\"}"
        params = {
            "noList": self.config.dbNo,
            "startTime": startTime,
            "endTime": endTime,
            "priceType": self.config.priceType,
            "plusPrice": plusPrice,
            "priceContent": priceContent,
            "proid": self.config.proId}
        response = self.req.send_request("post", url, params=params, headers=headers)
        if response["code"] == 200:
            db_changeAmount = response["data"][0]["changeAmount"]
            return db_changeAmount
        else:
            print(response['msg'], ",请重新输入！")

    def balance_retrieve(self, startTime=get_last_date(1), endTime=get_last_date(1), plusPrice=0.5,
                         priceContent=1.1):
        """
        执行余额退补，时间不填默认为昨日
        """
        url = self.SmalleelApi.dbretrieve
        headers = {"Authorization": self.smalleel_token, "Optpwd": self.smalleel_optPwd}
        if self.config.priceType == 2:
            priceContent = "{\"1\":\"1.1\",\"2\":\"2.2\",\"3\":\"3.3\",\"4\":\"4.4\"}"
        params = {
            "noList": self.config.dbNo,
            "startTime": startTime,
            "endTime": endTime,
            "executeType": 1,
            "priceType": self.config.priceType,
            "plusPrice": plusPrice,
            "priceContent": priceContent,
            "proid": self.config.proId,
            "isChangePrice": "true",
            "optPwd": self.smalleel_optPwd}
        response = self.req.send_request("post", url, params=params, headers=headers)
        if response["code"] == 200:
            print("余额退补,", response['msg'])
        else:
            print(f"操作失败，原因是:{response['msg']}")

    def balance_detail(self, startTime=get_last_date(1), endTime=get_last_date(1)):
        """
        获取余额退补详情，时间不填默认为昨日
        """
        url = self.SmalleelApi.balancedetail
        headers = {"Authorization": self.smalleel_token}
        aid = select_sql(read_sql_json('balance_sql', 'aid', 1, read_extract_json('proid')))[0][0]
        params = {
            "aid": aid,
            "startTime": startTime,
            "endTime": endTime}
        response = self.req.send_request("post", url, params=params, headers=headers,
                                         parameterFormat="data")
        if response["code"] == 200:
            detail_changeAmount = response['data']['changeAmount']
            if detail_changeAmount == self.__cost_detail():
                print('余额退补数据校验通过')
        else:
            print(f"操作失败，原因是:{response['msg']}")


if __name__ == '__main__':
    Login().login_smalleel()
    r = DbRetrieve()
    r.balance_retrieve()
    # pass
